
<table id="tbl-resv" class="table table-bordered table-hover table-condensed">
   <tr>
		<th>Total Room Cost</th>
		<td></td>
		<td><div class="input-prepend"><span class="add-on">Rp.</span><input id="room-cost" class="rp" type="text" value="<?= number_format($room['cost'],2,',','.') ?>" disabled></td>
   </tr>
   <tr>
		<th>Total Room Service Cost</th>
		<td></td>
		<td><div class="input-prepend"><span class="add-on">Rp.</span><input id="material-cost" class="rp" type="text" value="<?= number_format($material['cost'],2,',','.') ?>" disabled></div></td>
   </tr>
   <tr>
		<th>Extra Charge</th>		
		<td>
			<label class="radio"><input type="radio" name="charge" value="percent" <?php if($resv['extra_charge_percent'] != null) echo 'checked'; ?>> Percent</label>
			<label class="radio"><input type="radio" name="charge" value="nominal" <?php if($resv['extra_charge_nominal'] != null) echo 'checked'; ?>> Nominal</label>
			<div class="input-append" style="<?php if($resv['extra_charge_percent'] == null) echo 'display:none;'; ?>"><input id="charge-percent" class="percent" type="text" value="<?= $resv['extra_charge_percent'] ?>"><span class="add-on">%</span></div>
			<div class="input-prepend" style="<?php if($resv['extra_charge_nominal'] == null) echo 'display:none;'; ?>"><span class="add-on">Rp.</span><input id="charge-nominal" class="rp" type="text" value="<?= $resv['extra_charge_nominal'] ?>"></div>
		</td>
		<td><div class="input-prepend"><span class="add-on">Rp.</span><input id="rp-charge" class="rp" type="text" disabled></div></td>
   </tr>
   <tr>
		<th>Discount</th>
		<td>
			<label class="radio"><input type="radio" name="discount" value="percent" <?php if($resv['discount_percent'] != null) echo 'checked'; ?>> Percent</label>
			<label class="radio"><input type="radio" name="discount" value="nominal" <?php if($resv['discount_nominal'] != null) echo 'checked'; ?>> Nominal</label>
			<div class="input-append" style="<?php if($resv['discount_percent'] == null) echo 'display:none;'; ?>"><input id="disc-percent" class="percent" type="text" value="<?= $resv['discount_percent'] ?>"><span class="add-on">%</span></div>
			<div class="input-prepend" style="<?php if($resv['discount_nominal'] == null) echo 'display:none;'; ?>"><span class="add-on">Rp.</span><input id="disc-nominal" class="rp" type="text" value="<?= $resv['discount_nominal'] ?>"></div>
		</td>
		<td><div class="input-prepend"><span class="add-on">Rp.</span><input id="rp-disc" class="rp" type="text" disabled></div></td>
   </tr>
   <tr>
		<th>PPn Tax</th>
		<td><div class="input-append"><input id="ppn-percent" class="percent" type="text" value="<?= $resv['ppn_tax'] ?>"><span class="add-on">%</span></div></td>
		<td><div class="input-prepend"><span class="add-on">Rp.</span><input id="rp-ppn" class="rp" type="text" disabled></div></td>
   </tr>
   <tr>
		<th>Service Tax</th>
		<td><div class="input-append"><input id="service-percent" class="percent" type="text" value="<?= $resv['service_tax'] ?>"><span class="add-on">%</span></div></td>
		<td><div class="input-prepend"><span class="add-on">Rp.</span><input id="rp-service" class="rp" type="text" disabled></div></td>
   </tr>
    <tr>
		<th>Sub Total</th>
		<td></td>
		<td><div class="input-prepend"><span class="add-on">Rp.</span><input id="sub" class="rp" type="text" disabled></div></td>
   </tr>
   <tr>
		<th>Total Service Cost</th>
		<td></td>
		<td><div class="input-prepend"><span class="add-on">Rp.</span><input id="service-cost" class="rp" type="text" value="<?= number_format($service['cost'],2,',','.') ?>" disabled></td>
   </tr>
    <tr>
		<th>Grand Total</th>
		<td></td>
		<td><div class="input-prepend"><span class="add-on">Rp.</span><input id="grand" class="rp" type="text" disabled></div></td>
   </tr>
   <tr>
	<th>Description</th>
	<td><textarea id="desc" name="desc" maxlength="50"><?= $resv['resv_desc'] ?></textarea></td>
	<td></td>
   </tr>
</table>
<?php 
	if($is_cancelable || ($resv['ppn_tax'] == null && $resv['service_tax'] == null)) echo '<a class="btn btn-primary" href="javascript:void(0)" onclick="save()">Save</a>';
	else echo '<a class="btn btn-primary" href="javascript:void(0)" disabled>Save</a>';
?>
&nbsp;<a href="<?= base_url('reservation') ?>" class="btn">Back to Reservation</a>
<br><br>
<h3>Transactions</h3>
<a class="btn btn-primary" href="javascript:void(0)" onclick="add_trans();"><i class="icon-plus icon-white"></i> Add Transaction</a>
<br><br>
<table class="table table-bordered table-hover table-condensed">
    <thead>
        <tr>
            <th>Payment Type</th>
            <th>Ammount</th>
            <th>Description</th>
			<th>Voucher/Card Code</th>
        </tr>
    </thead>
    <?php
    $total = 0;
    foreach ($payment as $row){
        echo '<tr>';
        echo '<td>'.$row['payment_name'].'</td>';
        echo '<td>'.'Rp '.number_format($row['resv_paym_total'], 2, ',', '.').'</td>'.'<td>'.$row['resv_paym_desc'].'</td>';
		echo '<td>'.$row['special_code'].'</td>';
        echo '</tr>';
        
        $total = $total + $row['resv_paym_total'];
    }
	
	echo '<tr>';
	echo '<th>Balance</th>';
	echo '<th>Rp. <span id="balance" class="rp">0</span></th>';
	echo '<td></td><td></td></tr>';
    ?>
    
</table>
<a class="btn btn-success" href="<?php echo base_url(); ?>pdf_report/index/<?php echo $resv_id;?>" target="_blank"><i class="icon-print icon-white"></i> Print Receipt</a>
<a class="btn btn-success" href="<?php echo base_url(); ?>pdf_invoice/index/<?php echo $resv_id;?>" target="_blank"><i class="icon-print icon-white"></i> Print Invoice</a>
<br><br>

<script>
	
	var rp = {aSep: '.',aDec: ',',vMin: '0.00',vMax: '999999999999.99'};	
	var percent = {aSep: '',aDec: ',',vMin: '0.00',vMax: '100.00', wEmpty: 'zero'};
	var total_paym = <?= $total; ?>;
	
	$(document).ready(function() {
		$('.percent').autoNumeric('init',percent);
		$('.rp').autoNumeric('init',rp);
		$('input[name="charge"]').change(function(e) {charge(e);});	
		$('input[name="discount"]').change(function(e) {discount(e);});	
		$('#charge-percent,#charge-nominal,#disc-percent,#disc-nominal,#ppn-percent,#service-percent,input[name="charge"],input[name="discount"]').change(function() {calculate();});
		calculate();
	});
	
	function charge(id) {
		if($(id.target).val() == 'percent') {
			$('#charge-percent').parent().css('display','table-row');
			$('#charge-nominal').parent().css('display','none');
		}
		else {
			$('#charge-nominal').parent().css('display','table-row');
			$('#charge-percent').parent().css('display','none');
		}
	}
	
	function discount(id) {
		if($(id.target).val() == 'percent') {
			$('#disc-percent').parent().css('display','table-row');
			$('#disc-nominal').parent().css('display','none');
		}
		else {
			$('#disc-nominal').parent().css('display','table-row');
			$('#disc-percent').parent().css('display','none');
		}
	}
	
	function calculate() {
		
		var total = parseFloat($('#room-cost').autoNumeric('get')) + parseFloat($('#material-cost').autoNumeric('get'));
		
		if($('input[name="charge"]').filter('[value="percent"]').is(':checked')) {
			var charge = parseFloat($('#charge-percent').autoNumeric('get')) * total / 100;
		}
		else if($('input[name="charge"]').filter('[value="nominal"]').is(':checked')){
			var charge = parseFloat($('#charge-nominal').autoNumeric('get'));
		}
		else var charge = 0;
		
		total = total + charge;
		$('#rp-charge').autoNumeric('set', charge);
		
		if($('input[name="discount"]').filter('[value="percent"]').is(':checked')) {
			var disc = parseFloat($('#disc-percent').autoNumeric('get')) * total / 100;
		}
		else if($('input[name="discount"]').filter('[value="percent"]').is(':checked')) {
			var disc = parseFloat($('#disc-nominal').autoNumeric('get'));
		}
		else var disc = 0;
		total = total - disc;
		$('#rp-disc').autoNumeric('set', disc);
		
		var ppn = parseFloat($('#ppn-percent').autoNumeric('get')) * total / 100;
		$('#rp-ppn').autoNumeric('set', ppn);
		var service = parseFloat($('#service-percent').autoNumeric('get')) * total / 100;
		$('#rp-service').autoNumeric('set', service);
		total = total + ppn + service;
		$('#sub').autoNumeric('set', total);
		total = total + parseFloat($('#service-cost').autoNumeric('get'));
		$('#grand').autoNumeric('set', total);
		
		var balance = total-total_paym;
		$('#balance').autoNumeric('set', balance);
	}
	
	function add_trans() {
		 window.location.assign('<?php echo base_url(); ?>reservation/addPayment/<?php echo $resv_id; ?>/'+$('#balance').autoNumeric('get'));
	}
	
	function save() {
		var c = confirm('Extra charge, Discount, PPn Tax, Service Tax, and Description will be saved, continue?');
		if(!c) return false;
        $.post('<?php echo base_url(); ?>reservation/edit_pay/', {
            charge: $('input[name="charge"]').filter(':checked').val(),
            disc: $('input[name="discount"]').filter(':checked').val(),
            charge_percent: $('#charge-percent').autoNumeric('get'),
            charge_nominal: $('#charge-nominal').autoNumeric('get'),
            disc_percent: $('#disc-percent').autoNumeric('get'),
            disc_nominal: $('#disc-nominal').autoNumeric('get'),
            ppn: $('#ppn-percent').autoNumeric('get'),
            service: $('#service-percent').autoNumeric('get'),
            resv_id: <?php echo $resv_id; ?>,
			desc: $('#desc').val()
        }, function() {
			alert('Data Updated');
            window.location.reload();
        });
    }
	
	var myApp;
	myApp = myApp || (function () {
		var pleaseWaitDiv = $('<div class="modal hide" id="pleaseWaitDialog" data-backdrop="static" data-keyboard="false"><div class="modal-header"><h3>Processing...</h3></div><div class="modal-body"><div class="progress progress-striped active"><div class="bar" style="width: 100%;"></div></div></div></div>');
		return {
			showPleaseWait: function() {
				pleaseWaitDiv.modal();
			},
			hidePleaseWait: function () {
				pleaseWaitDiv.modal('hide');
			},

		};
	})();
</script>